
/******************************************************************************
 * Include files
 ******************************************************************************/
#include "../inc/main.h"
#include "init.h"
#include "global.h"
#include "motor.h"
#include "compu.h"
#include "control.h"

void PotentialCheck(void)
{
	unsigned char cc=0,i,j;
	
	//连续检测电位器检测，10次中有8次不归零，让为故障
	  for(i=0;i<10;i++)
	   { 
			 SampleSpeed();
	     if(SetSpeed!=0)cc++;
			 for(j=0;j<20;j++);
		 }
		 if(cc>8)ErrorCode=10; //上电电位器不归零	  
}

unsigned int hte=0;
int32_t main(void)
{	
	unsigned char DZCount=0;
	
	KKN=(MAXSPEED-MINSPEED);
	KKN=KKN/(NMAXVD-NMINVD);
	KPWMN=PWMMax-PWMMin;
	KPWMN=KPWMN/(NMAXVD-NMINVD);
	
 RCC_Configuration();
 LEDConfiguration();
 DR_Configuration();
 EN_Configuration();
 FG_Configuration();
 ADC_Configuration();
 BLDC_Configuration();
 BTIM1_Configuration();
 SENSORLESS_TIM_Config();
	
	for(DZCount=0;DZCount<200;DZCount++);
  DIin=SampleData[0];
	DZCount=0;
	PotentialCheck();
  while(1)
    {
       if(TimeCountCompuSpeed>20) //20MS采集一次速度设定值,并检查HALL是否有误
	       {
					 TimeCountCompuSpeed=0;SampleSpeed();					 
	       }
				 
			  		
		 if(Icount>=2)
		 {
			 Icount=0;
			 SampleOI();
		 }
		 
			if(TimeCountVI>=100)//100MS计算一次电压电流,并作一次实时速度计算
		  {
			 TimeCountVI=0;
			 SampleV();
			 SampleI();
			 			
         //	RealS实际转速需*2		
			 	RealS=hte*100/MPolePairs;//HALLcount1*10*60/6/MPolePairs; 100MS存储一次霍尔边沿数							
			  if(MOTORSTATE==STATESTARTOPEN||MOTORSTATE==STATERUNOPEN)
				 {
					 if(RealS<5)
						{
							 DZCount++;
							 if(DZCount>=50)//5S保护
							 { 								
									DZCount=0;ErrorCode=7;//堵转							 
								}
						}
						else DZCount=0;
					}
		  }		
		
		  if(ErrorCode!=0&&MOTORSTATE!=STATEERROR&&MOTORSTATE!=STATEERROROVER)//发生故障
		  {  
				  MOTORSTATE=STATEERROR;
    	}
			 
			switch(MOTORSTATE)
		 {
			 case STATESTARTCHECK:  				 
				    MotorStartCheck();			      
						break;
			 case STATESTARTDELAY:
						MotorStartDealy();
						ENCheck();
						break;
			 case STATESTARTOPEN:
						MotorStartOPEN();	
			      ENCheck();
			      break;			
			 case STATERUNOPEN: 
						MotorRunOPEN();
			      ENCheck();
						break;					 
			 case STATESTOP: MotorStop();break;
			 case STATEERROR: MotorError();break;
			 case STATEERROROVER: MotorErrorOver();break;
			 case STATWAITSTART:WaitStart();break;
	   }
    }
}


void BTIM1_IRQHandler(void)
{
  /* USER CODE BEGIN */
  if (BTIM_GetITStatus(CW_BTIM1, BTIM_IT_UPDATE))
  {
     BTIM_ClearITPendingBit(CW_BTIM1, BTIM_IT_UPDATE); 
								
Icount++;		
		TimeCountTemp++;		//临时计时用
		TimeCountCompuSpeed++; //计算设定速度
    TimeCountSTDelay++;		//启动延时
			
		TimeCountRealSpd++; //瞬时速度计明用
		if(TimeCountRealSpd>=20){HALLcountTemp=HALLcount;HALLcount=0;TimeCountRealSpd=0;}
		
	  TimeCountPID++; //PID定时计数用		
		TimeCountAvgSpd++;	//平均速度计数用
		if(TimeCountAvgSpd>=100){hte=HALLcount1;HALLcount1=0;TimeCountAvgSpd=0;}//用于显示的速度值或堵转保护的速度		 
		TimeCountStartUP++; //电机启动过程时，加速计数计时用		 
		TimeCountVI++;//计算电压电流
	}
  /* USER CODE END */
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
 /**
   * @brief  Reports the name of the source file and the source line number
   *         where the assert_param error has occurred.
   * @param  file: pointer to the source file name
   * @param  line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
       /* USER CODE END 6 */
}
#endif

